{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 连续和离散型特征的树的构建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "\n",
    "def loadDataSet(fileName):      #general function to parse tab -delimited floats\n",
    "    dataMat = []                #assume last column is target value\n",
    "    fr = open(fileName)\n",
    "    for line in fr.readlines():\n",
    "        curLine = line.strip().split('\\t')\n",
    "        fltLine = list(map(float,curLine)) #map all elements to float()\n",
    "        dataMat.append(fltLine)\n",
    "    return dataMat\n",
    "\n",
    "def binSplitDataSet(dataSet, feature, value):\n",
    "    mat0 = dataSet[nonzero(dataSet[:,feature] > value)[0],:]\n",
    "    mat1 = dataSet[nonzero(dataSet[:,feature] <= value)[0],:]\n",
    "    return mat0,mat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.,  0.,  0.,  0.],\n",
       "        [ 0.,  1.,  0.,  0.],\n",
       "        [ 0.,  0.,  1.,  0.],\n",
       "        [ 0.,  0.,  0.,  1.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testMat = mat(eye(4))\n",
    "testMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mat0,mat1 = binSplitDataSet(testMat, 1, 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 0.,  1.,  0.,  0.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.,  0.,  0.,  0.],\n",
       "        [ 0.,  0.,  1.,  0.],\n",
       "        [ 0.,  0.,  0.,  1.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回归树的切分函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def regLeaf(dataSet):#returns the value used for each leaf\n",
    "    return mean(dataSet[:,-1])\n",
    "\n",
    "def regErr(dataSet):\n",
    "    return var(dataSet[:,-1]) * shape(dataSet)[0]\n",
    "\n",
    "def chooseBestSplit(dataSet, leafType=regLeaf, errType=regErr, ops=(1,4)):\n",
    "    tolS = ops[0]; tolN = ops[1]\n",
    "    #if all the target variables are the same value: quit and return value\n",
    "    if len(set(dataSet[:,-1].T.tolist()[0])) == 1: #exit cond 1\n",
    "        return None, leafType(dataSet)\n",
    "    m,n = shape(dataSet)\n",
    "    #the choice of the best feature is driven by Reduction in RSS error from mean\n",
    "    S = errType(dataSet)\n",
    "    bestS = inf; bestIndex = 0; bestValue = 0\n",
    "    for featIndex in range(n-1):\n",
    "        for splitVal in set(dataSet[:,featIndex].T.tolist()[0]):\n",
    "            mat0, mat1 = binSplitDataSet(dataSet, featIndex, splitVal)\n",
    "            if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN): continue\n",
    "            newS = errType(mat0) + errType(mat1)\n",
    "            if newS < bestS: \n",
    "                bestIndex = featIndex\n",
    "                bestValue = splitVal\n",
    "                bestS = newS\n",
    "    #if the decrease (S-bestS) is less than a threshold don't do the split\n",
    "    if (S - bestS) < tolS: \n",
    "        return None, leafType(dataSet) #exit cond 2\n",
    "    mat0, mat1 = binSplitDataSet(dataSet, bestIndex, bestValue)\n",
    "    if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN):  #exit cond 3\n",
    "        return None, leafType(dataSet)\n",
    "    return bestIndex,bestValue#returns the best feature to split on\n",
    "                              #and the value used for that split\n",
    "\n",
    "def createTree(dataSet, leafType=regLeaf, errType=regErr, ops=(1,4)):#assume dataSet is NumPy Mat so we can array filtering\n",
    "    feat, val = chooseBestSplit(dataSet, leafType, errType, ops)#choose the best split\n",
    "    if feat == None: return val #if the splitting hit a stop condition return val\n",
    "    retTree = {}\n",
    "    retTree['spInd'] = feat\n",
    "    retTree['spVal'] = val\n",
    "    lSet, rSet = binSplitDataSet(dataSet, feat, val)\n",
    "    retTree['left'] = createTree(lSet, leafType, errType, ops)\n",
    "    retTree['right'] = createTree(rSet, leafType, errType, ops)\n",
    "    return retTree  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myDat = loadDataSet('ex00.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myMat = mat(myDat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD8CAYAAAB+UHOxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X+QXlV5B/Dvk80b3UWHF5qo8MKStENDTSlZ2QJjnA6k\n1ghU2BYUEGvrdCZDWzuVcTKurSOhdabbyXS0ViuTdhjL4GDU2G2YYFNtaLFpo25MQowlbQoS8kIL\nIhsLWXB3c/rHvne5e99z7j333nN/fz8zjNnd933vvRtznnOec85zRCkFIiJqnmVF3wARERWDAYCI\nqKEYAIiIGooBgIiooRgAiIgaigGAiKihGACIiBqKAYCIqKEYAIiIGmp50TcQZuXKlWr16tVF3wYR\nUWUcOHDgh0qpVTavLXUAWL16Naampoq+DSKiyhCRJ21fyxQQEVFDMQAQETUUAwARUUMxABARNRQD\nABFRQzEAEBE1VKmXg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C4DEAVA4DN834AwIRaSJAfF5EnAFwC4Nv53GLucmmv6pIC+g6Ai0Vk\njYisAHArgF2B1+wC8P7e7PpVAE4ppZ7J+0YdiXxeERkG8FUAv1GDXmHk8yql1iilViulVgP4CoDf\nrWjjD9j9//nvAbxNRJaLyBCAKwH8R8736YrN857AwmgHIvJGAGsBPJ7rXeYrl/aqFiMApdSciHwQ\nwB4srCi4Vyl1VETu6P38HiysDLkOwHEAp7HQo6gky+f9OICfAvBXvV7xnKpoUS3L560Nm+dVSv2H\niPwDgEcBnAHwN0op7ZLCsrP8+/0TAJ8XkSNYWBnzEaVUZauEisgDAK4GsFJETgK4C0ALyLe94k5g\nIqKGqksKiIiIYmIAICJqKAYAIqKGYgAgImooBgAiooZiACAiaigGACKihmIAICJqqP8HaqduY8Z6\nfqoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7de7a90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.scatter(array(myDat)[:,0], array(myDat)[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': 1.0180967672413792,\n",
       " 'right': -0.044650285714285719,\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.48813}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myDat1 = loadDataSet('ex0.txt')\n",
    "myMat1 = mat(myDat1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz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dNAP+qqu8CeFdEvg5gO4AiBm6b6/1lABO6WAB+VUS+A2ALgL/PZomZyyxe\n9bJU8g8APiIi14nIWgCfA3DE85wjAH5p6W7tTgAXVPW7WS80IaHXKyIbATwH4Bf7IAsLvV5VvU5V\nN6nqJgDPAvj1ggZtwO7/z38L4KdEZI2IDAK4CcArGa8zKTbXew6Lv11ARH4IwGYA3850ldnKLF71\nLONW1Usi8l8AHMXiHeovqeppEfm1pZ//ORY7DfYCeBXAPBa/wQvJ8np/F8APAPjTpSz0khZ0UI/l\n9fYNm+tV1VdE5H8B+CaABQBfVFXf1rK8s/z7/T0Afykip7DYafHbqlrYiYEi8hSAjwO4UkTeBLAf\nQBnIPl52hycxAAAAOUlEQVRx5yQRUcFw5yQRUcEwcBMRFQwDNxFRwTBwExEVDAM3EVHBMHATERUM\nAzcRUcEwcBMRFcx/AMSizNBOCR5eAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7de7eb8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(array(myDat1)[:,1], array(myDat1)[:,2])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': {'left': {'left': 3.9871631999999999,\n",
       "   'right': 2.9836209534883724,\n",
       "   'spInd': 1,\n",
       "   'spVal': 0.797583},\n",
       "  'right': 1.980035071428571,\n",
       "  'spInd': 1,\n",
       "  'spVal': 0.582002},\n",
       " 'right': {'left': 1.0289583666666666,\n",
       "  'right': -0.023838155555555553,\n",
       "  'spInd': 1,\n",
       "  'spVal': 0.197834},\n",
       " 'spInd': 1,\n",
       " 'spVal': 0.39435}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': 1.035533,\n",
       "                'right': 1.077553,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.993349},\n",
       "               'right': {'left': 0.74420699999999995,\n",
       "                'right': 1.069062,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.988852},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.989888},\n",
       "              'right': 1.227946,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.985425},\n",
       "             'right': {'left': {'left': 0.86291099999999998,\n",
       "               'right': 0.67357900000000004,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.975022},\n",
       "              'right': {'left': {'left': 1.0646899999999999,\n",
       "                'right': {'left': 0.94525499999999996,\n",
       "                 'right': 1.0229060000000001,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.950153},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.951949},\n",
       "               'right': {'left': 0.63186200000000003,\n",
       "                'right': {'left': {'left': 1.0262579999999999,\n",
       "                  'right': 1.0356449999999999,\n",
       "                  'spInd': 0,\n",
       "                  'spVal': 0.930173},\n",
       "                 'right': 0.88322500000000004,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.928097},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.936783},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.948268},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.953112},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.976414},\n",
       "            'right': {'left': {'left': {'left': {'left': 1.0298890000000001,\n",
       "                'right': 1.123413,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.919074},\n",
       "               'right': {'left': 0.86160099999999995,\n",
       "                'right': {'left': 1.0559000000000001,\n",
       "                 'right': 0.99687099999999995,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.900272},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.901056},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.902532},\n",
       "              'right': {'left': 1.2402089999999999,\n",
       "               'right': {'left': 1.077275,\n",
       "                'right': 1.1178330000000001,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.884512},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.89593},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.897094},\n",
       "             'right': {'left': {'left': {'left': 0.79700499999999996,\n",
       "                'right': 1.114825,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.869077},\n",
       "               'right': 0.71748999999999996,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.860049},\n",
       "              'right': 1.1709590000000001,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.848921},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.877241},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.919384},\n",
       "           'right': {'left': 0.72002999999999995,\n",
       "            'right': 0.95261700000000005,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.845815},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.846455},\n",
       "          'right': {'left': {'left': 1.229373,\n",
       "            'right': {'left': 1.01058,\n",
       "             'right': {'left': 1.0821529999999999,\n",
       "              'right': 1.0866480000000001,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.822443},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.824442},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.834078},\n",
       "           'right': {'left': 1.2808949999999999,\n",
       "            'right': 1.3259069999999999,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.820802},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.821648},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.837522},\n",
       "         'right': {'left': {'left': {'left': {'left': 0.83526400000000001,\n",
       "             'right': 1.0952059999999999,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.814825},\n",
       "            'right': {'left': 0.70660100000000003,\n",
       "             'right': {'left': 0.92403299999999999,\n",
       "              'right': 0.96572100000000005,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.795072},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.804586},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.813719},\n",
       "           'right': {'left': 0.53321399999999997,\n",
       "            'right': 0.55261400000000005,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.789625},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.79024},\n",
       "          'right': {'left': {'left': {'left': {'left': {'left': 1.1652960000000001,\n",
       "               'right': {'left': {'left': 0.88604899999999998,\n",
       "                 'right': 1.0744880000000001,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.78193},\n",
       "                'right': 0.83676300000000003,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.774301},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.782167},\n",
       "              'right': {'left': {'left': 1.1259429999999999,\n",
       "                'right': 1.140917,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.773168},\n",
       "               'right': 1.299018,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.772083},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.773422},\n",
       "             'right': {'left': {'left': {'left': {'left': 0.89970499999999998,\n",
       "                 'right': 0.76021899999999998,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.768596},\n",
       "                'right': 1.058262,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.761474},\n",
       "               'right': {'left': 0.74810399999999999,\n",
       "                'right': 0.90629099999999996,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.750078},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.750918},\n",
       "              'right': {'left': {'left': 1.087056,\n",
       "                'right': 1.2007810000000001,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.737189},\n",
       "               'right': {'left': 0.93195600000000001,\n",
       "                'right': {'left': 1.000567,\n",
       "                 'right': 1.017112,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.726828},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.727098},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.729234},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.742527},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.768784},\n",
       "            'right': 1.307248,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.72312},\n",
       "           'right': {'left': {'left': 0.93349000000000004,\n",
       "             'right': 0.56485799999999997,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.712386},\n",
       "            'right': {'left': {'left': {'left': 1.1016779999999999,\n",
       "               'right': 0.82780500000000001,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.697777},\n",
       "              'right': 1.212434,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.697718},\n",
       "             'right': {'left': 0.84542300000000004,\n",
       "              'right': 0.83439099999999999,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.691115},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.696648},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.703755},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.712503},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.785541},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.819823},\n",
       "        'right': {'left': 1.414382,\n",
       "         'right': {'left': {'left': 0.99998500000000001,\n",
       "           'right': 1.3072170000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.67939},\n",
       "          'right': {'left': {'left': 0.90772699999999995,\n",
       "            'right': 0.91507700000000003,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.673195},\n",
       "           'right': 1.1871290000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.66387},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.678287},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.683886},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.683921},\n",
       "       'right': {'left': {'left': 0.70163399999999998,\n",
       "         'right': 0.76704000000000006,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.661923},\n",
       "        'right': {'left': 0.95850599999999997,\n",
       "         'right': 1.004346,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.652121},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.656218},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.6632},\n",
       "      'right': {'left': {'left': 1.3153840000000001,\n",
       "        'right': 1.287407,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.648675},\n",
       "       'right': {'left': 1.026886,\n",
       "        'right': 1.024241,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.643665},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.645762},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.651376},\n",
       "     'right': {'left': 0.78255200000000003,\n",
       "      'right': 0.84054399999999996,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.626011},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.643601},\n",
       "    'right': 1.244731,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.625791},\n",
       "   'right': {'left': 0.62369600000000003,\n",
       "    'right': 0.76632999999999996,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.622398},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.625336},\n",
       "  'right': {'left': {'left': 1.3344210000000001,\n",
       "    'right': 1.6210910000000001,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.613765},\n",
       "   'right': {'left': {'left': {'left': {'left': {'left': {'left': {'left': 0.98203600000000002,\n",
       "          'right': 1.212685,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.604529},\n",
       "         'right': 0.97477000000000003,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.597409},\n",
       "        'right': {'left': 1.213435,\n",
       "         'right': 1.3366610000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.59021},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.595012},\n",
       "       'right': {'left': 0.70553100000000002,\n",
       "        'right': {'left': {'left': {'left': 1.1858120000000001,\n",
       "           'right': 0.92188499999999995,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.578252},\n",
       "          'right': 1.234129,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.576946},\n",
       "         'right': {'left': 0.89908999999999994,\n",
       "          'right': {'left': {'left': 1.06613,\n",
       "            'right': 0.96905799999999997,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.567704},\n",
       "           'right': 1.0705290000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.561362},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.574573},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.575805},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.589575},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.590062},\n",
       "      'right': {'left': {'left': {'left': 1.2531509999999999,\n",
       "         'right': 1.391273,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.55352},\n",
       "        'right': 1.0361579999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.55299},\n",
       "       'right': 1.3696299999999999,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.552381},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.559763},\n",
       "     'right': {'left': {'left': {'left': 0.89374799999999999,\n",
       "        'right': 1.0538460000000001,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.539558},\n",
       "       'right': {'left': {'left': 0.86728400000000005,\n",
       "         'right': 0.89346199999999998,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.530897},\n",
       "        'right': {'left': 1.0222059999999999,\n",
       "         'right': 0.87956000000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.527505},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.529491},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.536689},\n",
       "      'right': {'left': 1.209557,\n",
       "       'right': {'left': 0.96198300000000003,\n",
       "        'right': 1.0371790000000001,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.518735},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.520044},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.520207},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.541314},\n",
       "    'right': {'left': 1.4935860000000001,\n",
       "     'right': {'left': 1.1566479999999999,\n",
       "      'right': 1.1072649999999999,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.50794},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.514563},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.517921},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.61127},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.620599},\n",
       " 'right': {'left': {'left': {'left': {'left': {'left': -0.097791000000000003,\n",
       "      'right': {'left': -0.16370699999999999,\n",
       "       'right': -0.15293999999999999,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.465625},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.475976},\n",
       "     'right': {'left': {'left': -0.061455999999999997,\n",
       "       'right': {'left': 0.069098000000000007,\n",
       "        'right': {'left': 0.026974000000000001,\n",
       "         'right': 0.034014000000000003,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.438367},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.448656},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.44928},\n",
       "      'right': -0.18865899999999999,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.429664},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.458121},\n",
       "    'right': 0.33172200000000002,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.41023},\n",
       "   'right': {'left': {'left': {'left': {'left': -0.366317,\n",
       "       'right': {'left': -0.12164,\n",
       "        'right': -0.29609400000000002,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.378595},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.401152},\n",
       "      'right': {'left': 0.088505,\n",
       "       'right': -0.24354999999999999,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.377201},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.377597},\n",
       "     'right': {'left': -0.55646399999999996,\n",
       "      'right': -0.20483000000000001,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.360323},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.362314},\n",
       "    'right': {'left': {'left': {'left': -0.11939900000000001,\n",
       "       'right': 0.048939000000000003,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.348013},\n",
       "      'right': {'left': {'left': -0.15340500000000001,\n",
       "        'right': -0.15038899999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.346986},\n",
       "       'right': -0.061539000000000003,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.344102},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.347837},\n",
       "     'right': -0.37169999999999997,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.343554},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.355688},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.406649},\n",
       "  'right': {'left': {'left': {'left': 0.175264,\n",
       "     'right': 0.20678299999999999,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.339563},\n",
       "    'right': {'left': 0.026332000000000001,\n",
       "     'right': 0.21008399999999999,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.332982},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.3371},\n",
       "   'right': {'left': {'left': {'left': {'left': {'left': {'left': -0.219245,\n",
       "         'right': {'left': 0.180811,\n",
       "          'right': {'left': {'left': {'left': {'left': {'left': -0.001952,\n",
       "               'right': {'left': -0.17732100000000001,\n",
       "                'right': {'left': -0.115991,\n",
       "                 'right': -0.14865,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.302217},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.30554},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.306964},\n",
       "              'right': {'left': 0.317135,\n",
       "               'right': 0.002882,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.295511},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.302001},\n",
       "             'right': -0.22888,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.280738},\n",
       "            'right': 0.25362800000000002,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.278661},\n",
       "           'right': {'left': {'left': -0.085712999999999998,\n",
       "             'right': {'left': -0.45521899999999998,\n",
       "              'right': -0.16597100000000001,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.269681},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.273147},\n",
       "            'right': {'left': 0.073446999999999998,\n",
       "             'right': {'left': -0.055613000000000003,\n",
       "              'right': {'left': 0.046296999999999998,\n",
       "               'right': -0.029467,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.243909},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.250744},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.252649},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.268857},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.27394},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.314924},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.323181},\n",
       "        'right': 0.20935899999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.242204},\n",
       "       'right': {'left': {'left': -0.35845900000000003,\n",
       "         'right': -0.34814699999999998,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.233115},\n",
       "        'right': {'left': {'left': -0.0068989999999999998,\n",
       "          'right': {'left': {'left': -0.064035999999999996,\n",
       "            'right': -0.087744000000000003,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.202054},\n",
       "           'right': -0.048847000000000002,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.196005},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.203693},\n",
       "         'right': -0.32758900000000002,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.193641},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.210334},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.23807},\n",
       "      'right': {'left': {'left': {'left': {'left': {'left': 0.11368499999999999,\n",
       "           'right': 0.18484300000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.18351},\n",
       "          'right': {'left': 0.103676,\n",
       "           'right': 0.132858,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.152324},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.180506},\n",
       "         'right': 0.20429800000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.148049},\n",
       "        'right': {'left': 0.034283000000000001,\n",
       "         'right': 0.13697899999999999,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.145809},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.146366},\n",
       "       'right': {'left': {'left': -0.22314300000000001,\n",
       "         'right': -0.329372,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.132543},\n",
       "        'right': {'left': {'left': {'left': 0.18424099999999999,\n",
       "           'right': -0.026166999999999999,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.130052},\n",
       "          'right': 0.30510700000000002,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.129061},\n",
       "         'right': {'left': {'left': {'left': -0.077409000000000006,\n",
       "            'right': {'left': 0.068834000000000006,\n",
       "             'right': 0.02528,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.099142},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.101149},\n",
       "           'right': -0.33276,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.098016},\n",
       "          'right': {'left': 0.22716700000000001,\n",
       "           'right': {'left': 0.099934999999999996,\n",
       "            'right': -0.019546999999999998,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.084248},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.091358},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.096994},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.118156},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.130962},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.1333},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.188218},\n",
       "     'right': {'left': {'left': -0.269756,\n",
       "       'right': {'left': -0.349692,\n",
       "        'right': -0.420983,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.072243},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.074795},\n",
       "      'right': {'left': {'left': -0.110946,\n",
       "        'right': 0.052439,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.066172},\n",
       "       'right': -0.30697000000000002,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.065615},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.071769},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.081931},\n",
       "    'right': {'left': {'left': 0.064495999999999998,\n",
       "      'right': {'left': 0.40815499999999999,\n",
       "       'right': 0.15509600000000001,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.036098},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.036492},\n",
       "     'right': {'left': -0.132525,\n",
       "      'right': {'left': 0.056593999999999998,\n",
       "       'right': {'left': {'left': 0.069975999999999997,\n",
       "         'right': 0.094149999999999998,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.007044},\n",
       "        'right': 0.060902999999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.000234},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.008307},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.009849},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.014083},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.048014},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.325412},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.343479},\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.48813}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat, ops=(0,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myDat2 = loadDataSet('ex2.txt')\n",
    "myMat2 = mat(myDat2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X9sXed5H/DvQ+rauXQKUZ5Vw2Z+SB1cqfE8SxXnBFMw\nWO4WJjYSq3YaGw3WoAjgDkuDxSiI0sMQSckf5qYVzoYtGdQgSId1tmzLZaQqrZxE6TK4dWISlGLL\nCTct/qVrN2YT0VvEa/uSfPbHvYc6PPf8eM857/l1z/cDCBYPD+/5Qes573ne531fUVUQEdHgGyr6\nBIiIKB8M+ERENcGAT0RUEwz4REQ1wYBPRFQTDPhERDXBgE9EVBMM+ERENcGAT0RUE5uKPgG3a665\nRrdt21b0aRARVcrc3NzfqerWqP1KFfC3bduG2dnZok+DiKhSROQlk/2Y0iEiqgkGfCKimmDAJyKq\nCQZ8IqKaYMAnIqqJUlXpEJGZmfkWDp9awKtLbVw/2sTkxA7s3z1W9GlRyTHgE1XMzHwLDzzxLNqd\nVQBAa6mNB554FgAY9CkUUzpEFXP41MJ6sHe0O6s4fGqhoDOiqmDAJ6qYV5fasbYTORjwiSrm+tFm\nrO1EDgZ8ooqZnNiBZmN4w7ZmYxiTEzsKOiOqCnbaElWM0zHLKh2KiwGfqIL27x5jgKfYmNIhIqoJ\nBnwioppgwCciqgkGfCKimmDAJyKqCQZ8IqKaYMAnIqoJBnwiopowDvgi8jUReV1EnnNtOygiLRE5\n0/tzu+t7D4jIeRFZEJEJ2ydORETxxGnhfx3Ah322P6Squ3p/vgkAIvI+APcCuLH3M18WkWGfnyUi\nopwYB3xV/R6AnxvufieAR1T1LVV9AcB5ALckOD8iIrL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FHnNIvIeAE8A+OcD0tqLvGZV3a6q21R1\nG4DHAfzLCgd7wOz/7W8A+KCIbBKREQDvB/CjnM/TFpPrfRndtxmIyLUAdgD4Sa5nmT/r8auyLXxV\nXRGR3wdwCt1e/q9pd/H0f9H7/n9Bt2LjdgDnASyj20qoLMNr/jyAvwfgy70W74pWeOIpw2seKCbX\nrKo/EpG/BPBDAGsAvqqqvuV9ZWf4O/4igK+LyLPoVq38oapWegZNEXkYwK0ArhGRCwAOAGgA2cUv\njrQlIqqJKqd0iIgoBgZ8IqKaYMAnIqoJBnwioppgwCciqgkGfCKimmDAJyKqCQZ8IqKa+P/uHGIF\nAalY9gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7de7b00>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(array(myDat2)[:,0], array(myDat2)[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': {'left': {'left': {'left': 105.24862350000001,\n",
       "    'right': 112.42895575000001,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.958512},\n",
       "   'right': {'left': {'left': {'left': {'left': 87.310387500000004,\n",
       "       'right': {'left': {'left': 96.452866999999998,\n",
       "         'right': {'left': 104.82540899999999,\n",
       "          'right': {'left': 95.181792999999999,\n",
       "           'right': 102.25234449999999,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.872883},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.892999},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.910975},\n",
       "        'right': 95.275843166666661,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.85497},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.944221},\n",
       "      'right': {'left': 81.110151999999999,\n",
       "       'right': 88.784498800000009,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.811602},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.833026},\n",
       "     'right': 102.35780185714285,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.790312},\n",
       "    'right': 78.085643250000004,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.759504},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.952833},\n",
       "  'right': {'left': {'left': {'left': 114.554706,\n",
       "     'right': {'left': 104.82495374999999,\n",
       "      'right': 108.92921799999999,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.698472},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.706961},\n",
       "    'right': 114.15162428571431,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.666452},\n",
       "   'right': {'left': 93.673449714285724,\n",
       "    'right': {'left': 123.2101316,\n",
       "     'right': {'left': 97.200180249999988,\n",
       "      'right': {'left': {'left': 109.38961049999999,\n",
       "        'right': 110.979946,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.543843},\n",
       "       'right': 101.73699325000001,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.51915},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.553797},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.582311},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.613004},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.640515},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.729397},\n",
       " 'right': {'left': {'left': 12.50675925,\n",
       "   'right': 3.4331330000000007,\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.467383},\n",
       "  'right': {'left': {'left': {'left': -12.558604833333334,\n",
       "     'right': {'left': 14.38417875,\n",
       "      'right': {'left': -0.89235549999999952,\n",
       "       'right': 3.6584772500000016,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.385021},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.412516},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.437652},\n",
       "    'right': {'left': {'left': -15.085111749999999,\n",
       "      'right': -22.693879600000002,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.350725},\n",
       "     'right': {'left': 15.059290750000001,\n",
       "      'right': {'left': -19.994155200000002,\n",
       "       'right': {'left': {'left': {'left': {'left': {'left': 0.40377471428571476,\n",
       "            'right': -13.070501,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.25807},\n",
       "           'right': 6.770429,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.228473},\n",
       "          'right': -11.822278500000001,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.217214},\n",
       "         'right': 3.4496025000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.202161},\n",
       "        'right': {'left': -12.107972500000001,\n",
       "         'right': -6.2479000000000013,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.156067},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.166765},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.297107},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.324274},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.335182},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.373501},\n",
       "   'right': {'left': 6.5098432857142843,\n",
       "    'right': {'left': -2.5443927142857148,\n",
       "     'right': 4.0916259999999998,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.044737},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.084661},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.126833},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.457563},\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.499171}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': 101.35815937735848,\n",
       " 'right': -2.6377193297872341,\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.499171}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat2, ops=(10000,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def isTree(obj):\n",
    "    return (type(obj).__name__=='dict')\n",
    "\n",
    "def getMean(tree):\n",
    "    if isTree(tree['right']): tree['right'] = getMean(tree['right'])\n",
    "    if isTree(tree['left']): tree['left'] = getMean(tree['left'])\n",
    "    return (tree['left']+tree['right'])/2.0\n",
    "    \n",
    "def prune(tree, testData):\n",
    "    if shape(testData)[0] == 0: return getMean(tree) #if we have no test data collapse the tree\n",
    "    if (isTree(tree['right']) or isTree(tree['left'])):#if the branches are not trees try to prune them\n",
    "        lSet, rSet = binSplitDataSet(testData, tree['spInd'], tree['spVal'])\n",
    "    if isTree(tree['left']): tree['left'] = prune(tree['left'], lSet)\n",
    "    if isTree(tree['right']): tree['right'] =  prune(tree['right'], rSet)\n",
    "    #if they are now both leafs, see if we can merge them\n",
    "    if not isTree(tree['left']) and not isTree(tree['right']):\n",
    "        lSet, rSet = binSplitDataSet(testData, tree['spInd'], tree['spVal'])\n",
    "        errorNoMerge = sum(power(lSet[:,-1] - tree['left'],2)) +\\\n",
    "            sum(power(rSet[:,-1] - tree['right'],2))\n",
    "        treeMean = (tree['left']+tree['right'])/2.0\n",
    "        errorMerge = sum(power(testData[:,-1] - treeMean,2))\n",
    "        if errorMerge < errorNoMerge: \n",
    "            print(\"merging\")\n",
    "            return treeMean\n",
    "        else: return tree\n",
    "    else: return tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.48672324076354673"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree = createTree(myMat)\n",
    "getMean(tree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myTree = createTree(myMat)\n",
    "myDatTest = loadDataSet('ex2test.txt')\n",
    "myMat2Test = mat(myDatTest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X+QHGd95/H3V6uxWZmcV8Qbn722kMjZ0tk4lqI94zoR\ngmxAYMdY2CljQoAAdYI6wsU+1wYZUrFJoBAxjnNXuXAniCtQB7YEIsJgQPyQA3cEA6uThC1s3/kn\neHDsDdZCbK3l3dX3/pgZaTTbPd093T3dM/N5Vamsne2dfnpW/vbT3+f7PI+5OyIi0v8WFd0AERHp\nDgV8EZEBoYAvIjIgFPBFRAaEAr6IyIBQwBcRGRAK+CIiA0IBX0RkQMQO+GZ2q5k9ZWb3Nr12o5lV\nzWxf/c8lTd+73sweNLMHzGxD1g0XEZFkLO5MWzN7BfAM8Gl3f2n9tRuBZ9z9Yy3HngPcBlwAnA58\nEzjb3efbneOUU07x5cuXJ7wEEZHBtmfPnn9299Go4xbHfUN3/46ZLY95+OXA7e5+GHjEzB6kFvy/\n1+6Hli9fzuTkZNwmiYgIYGaPxTkuixz+e83sR/WUz9L6a2PAT5uOebz+moiIFCRtwP848BJgNfAE\ncHPSNzCzTWY2aWaTU1NTKZsjIiJhUgV8d3/S3efd/QjwCWppG4AqcGbToWfUXwt6j63uPu7u46Oj\nkSkoERHpUKqAb2anNX35BqBRwXMHcLWZnWhmK4CzgB+kOZeIiKQTe9DWzG4DXgmcYmaPAzcArzSz\n1YADjwLvAnD3A2a2HfgxMAe8J6pCR0RE8hW7LLMbxsfHXVU6ItINO/dWuWnXA/xseobTR4aZ2LCS\njWt6s7bEzPa4+3jUcbF7+CIi/WLn3irXf+EeZmZriYfq9AzXf+EegJ4N+nEo4IsUqJ96mb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fd657xHF6CKeb19I871uvt9ZvY14EfA\nEeCT7h5Y4ld2MX+/fw78nZndQ61y5X3u3rMraJrZbcArgVPM7HHgBqAC3Y1XmmkrIjIgejWlIyIi\nCSngi4gMCAV8EZEBoYAvIjIgFPBFRAaEAr6IyIBQwBcRGRAK+CIiA+L/A09M9Bz87ui5AAAAAElF\nTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x8d7e550>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(array(myDatTest)[:,0], array(myDatTest)[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': 1.0180967672413792,\n",
       " 'right': -0.044650285714285719,\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.48813}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prune(myTree, myMat2Test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模型树"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型树的叶节点生产函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def linearSolve(dataSet):   #helper function used in two places\n",
    "    m,n = shape(dataSet)\n",
    "    X = mat(ones((m,n))); Y = mat(ones((m,1)))#create a copy of data with 1 in 0th postion\n",
    "    X[:,1:n] = dataSet[:,0:n-1]; Y = dataSet[:,-1]#and strip out Y\n",
    "    xTx = X.T*X\n",
    "    if linalg.det(xTx) == 0.0:\n",
    "        raise NameError('This matrix is singular, cannot do inverse,\\n\\\n",
    "        try increasing the second value of ops')\n",
    "    ws = xTx.I * (X.T * Y)\n",
    "    return ws,X,Y\n",
    "\n",
    "def modelLeaf(dataSet):#create linear model and return coeficients\n",
    "    ws,X,Y = linearSolve(dataSet)\n",
    "    return ws\n",
    "\n",
    "def modelErr(dataSet):\n",
    "    ws,X,Y = linearSolve(dataSet)\n",
    "    yHat = X * ws\n",
    "    return sum(power(Y - yHat,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myDat2 = loadDataSet('exp2.txt')\n",
    "myMat2 = mat(myDat2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGeVJREFUeJzt3X+Q3Hddx/HX+zbf0L0A3YOejFk4U3+lGkJTPCVDHaXF\naSqlEGJpxdYfiNNxHFEYDCRSSatlEici1XHUqRXRaacWS1hbfwXGojiVRC9uwhFpFMGm3YCNkqtj\nbiGby9s/9vayt9kf39397nf3+93nY4Zpbu+7t5/vpLzu0/f3/fl8zN0FAEi+iWEPAAAQDQIdAFKC\nQAeAlCDQASAlCHQASAkCHQBSgkAHgJQg0AEgJQh0AEiJNXF+2BVXXOEbNmyI8yMBIPGOHDny3+4+\n3em6WAN9w4YNmpubi/MjASDxzOzpMNdRcgGAlCDQASAlCHQASAkCHQBSomOgm9lHzOw5M/t83Wv7\nzewpM/ucmX3CzHKDHSYAoJMwM/SPSrqx4bVPSXqlu79K0r9J2h3xuAAAXerYtujunzGzDQ2vfbLu\ny0OSbol2WACQHHcV5vXw4We05K6Mmd72mlfo3u2bYx9HFH3oPy3pkVbfNLM7Jd0pSTMzMxF8HACM\njrsK83rw0MmVr5fc9eChk/rEv5S0eG5J63NZ7dy2UduvyQ98LH09FDWz90s6L+mhVte4+/3uPuvu\ns9PTHRc6AUCiPHz4maavnz23JJdUWihr94F5FYqlgY+l50A3s5+S9EZJtzsnTQMYU0sh4q9cWdL+\ngycGPpaeSi5mdqOk90r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6YC7n5Qkd0/yPYe5X5f0IjMzSS9UNdDP\nxzvMaLj7Z1QdfyuxZdWoBnpe0jN1Xz+7/Fq31yRFt/fyDlV/4ydVx/s1s7ykt0j6vRjHNShh/n6/\nU9KUmf2dmR0xs5+IbXTRC3O/vyPpuySdkjQv6Rfd/UI8w4tdbFmVqAMuIJnZdaoG+vcPeywDdp+k\n97n7heokLvXWSPoeSa+XlJX0WTM75O7/NtxhDcw2SUclXS/p2yR9ysz+wd3/d7jDSrZRDfSSpFfU\nff3y5de6vSYpQt2Lmb1K0gOSftjd/yemsQ1CmPudlfSny2F+haQ3mNl5dy/EM8RIhbnfZyX9j7uf\nlXTWzD4j6WpJSQz0MPf7dkn7vFpk/qKZfVnSVZL+KZ4hxiq2rBrVkss/S/oOM7vSzNZK+lFJjzVc\n85ikn1h+grxV0vPu/pW4BxqRjvdrZjOSDkj68RTM2jrer7tf6e4b3H2DpEcl/VxCw1wK9+/zn0v6\nfjNbY2aTkl4j6QsxjzMqYe73pKr/NSIze5mkjZK+FOso4xNbVo3kDN3dz5vZz0s6qOoT84+4+3Ez\n+9nl7/++qp0Pb5D0RUmLqv7GT6SQ9/sBSS+V9LvLs9bzntBNjkLeb2qEuV93/4KZ/Y2kz0m6IOkB\nd2/aBjfqQv79/pqkj5rZvKrdH+9z90TuwmhmD0t6naQrzOxZSXskBVL8WcVKUQBIiVEtuQAAukSg\nA0BKEOgAkBIEOgCkBIEOAClBoANAShDoAJASBDoApMT/A3msYJ9nYdSNAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x8de8fd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(array(myDat2)[:,0], array(myDat2)[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': matrix([[  1.69855694e-03],\n",
       "         [  1.19647739e+01]]), 'right': matrix([[ 3.46877936],\n",
       "         [ 1.18521743]]), 'spInd': 0, 'spVal': 0.285477}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat2, modelLeaf, modelErr, (1, 10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 示例：树回归与标准回归的比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def regTreeEval(model, inDat):\n",
    "    return float(model)\n",
    "\n",
    "def modelTreeEval(model, inDat):\n",
    "    n = shape(inDat)[1]\n",
    "    X = mat(ones((1,n+1)))\n",
    "    X[:,1:n+1]=inDat\n",
    "    return float(X*model)\n",
    "\n",
    "def treeForeCast(tree, inData, modelEval=regTreeEval):\n",
    "    if not isTree(tree): return modelEval(tree, inData)\n",
    "    if inData[tree['spInd']] > tree['spVal']:\n",
    "        if isTree(tree['left']): return treeForeCast(tree['left'], inData, modelEval)\n",
    "        else: return modelEval(tree['left'], inData)\n",
    "    else:\n",
    "        if isTree(tree['right']): return treeForeCast(tree['right'], inData, modelEval)\n",
    "        else: return modelEval(tree['right'], inData)\n",
    "        \n",
    "def createForeCast(tree, testData, modelEval=regTreeEval):\n",
    "    m=len(testData)\n",
    "    yHat = mat(zeros((m,1)))\n",
    "    for i in range(m):\n",
    "        yHat[i,0] = treeForeCast(tree, mat(testData[i]), modelEval)\n",
    "    return yHat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 创建回归树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.96408523182221406"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainMat = mat(loadDataSet('bikeSpeedVsIq_train.txt'))\n",
    "testMat = mat(loadDataSet('bikeSpeedVsIq_test.txt'))\n",
    "myTree = createTree(trainMat, ops=(1,20))\n",
    "yHat = createForeCast(myTree, testMat[:, 0])\n",
    "corrcoef(yHat, testMat[:, 1], rowvar=0)[0,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建模型树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9760412191380593"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myTree = createTree(trainMat, modelLeaf, modelErr, ops=(1,20))\n",
    "yHat = createForeCast(myTree, testMat[:,0],modelTreeEval)\n",
    "corrcoef(yHat, testMat[:, 1], rowvar=0)[0,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "标准线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 37.58916794],\n",
       "        [  6.18978355]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws, X, Y = linearSolve(trainMat)\n",
    "ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.94346842356747629"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(shape(testMat)[0]):\n",
    "    yHat[i] = testMat[i,0]*ws[1,0] + ws[0,0]\n",
    "\n",
    "corrcoef(yHat, testMat[:, 1], rowvar=0)[0,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用 Python 的 Tkinter 库创建 GUI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import tkinter as tk\n",
    "root = tk.Tk()\n",
    "myLabel = tk.Label(root, text='Hello,World')\n",
    "myLabel.grid()\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用于构建树管理器界面的Tkinter小部件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "\n",
    "from tkinter import *\n",
    "\n",
    "#import matplotlib\n",
    "#matplotlib.use('TkAgg')\n",
    "from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg\n",
    "from matplotlib.figure import Figure\n",
    "\n",
    "def reDraw(tolS,tolN):\n",
    "    reDraw.f.clf()        # clear the figure\n",
    "    reDraw.a = reDraw.f.add_subplot(111)\n",
    "    if chkBtnVar.get():\n",
    "        if tolN < 2: tolN = 2\n",
    "        myTree = createTree(reDraw.rawDat, modelLeaf, modelErr, (tolS,tolN))\n",
    "        yHat = createForeCast(myTree, reDraw.testDat, modelTreeEval)\n",
    "    else:\n",
    "        myTree=createTree(reDraw.rawDat, ops=(tolS,tolN))\n",
    "        yHat = createForeCast(myTree, reDraw.testDat)\n",
    "    reDraw.a.scatter(reDraw.rawDat[:,0].flatten().A[0], reDraw.rawDat[:,1].flatten().A[0], s=5) #use scatter for data set\n",
    "    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) #use plot for yHat\n",
    "    reDraw.canvas.show()\n",
    "    \n",
    "def getInputs():\n",
    "    try: tolN = int(tolNentry.get())\n",
    "    except: \n",
    "        tolN = 10 \n",
    "        print(\"enter Integer for tolN\")\n",
    "        tolNentry.delete(0, END)\n",
    "        tolNentry.insert(0,'10')\n",
    "    try: tolS = float(tolSentry.get())\n",
    "    except: \n",
    "        tolS = 1.0 \n",
    "        print(\"enter Float for tolS\")\n",
    "        tolSentry.delete(0, END)\n",
    "        tolSentry.insert(0,'1.0')\n",
    "    return tolN,tolS\n",
    "\n",
    "def drawNewTree():\n",
    "    tolN,tolS = getInputs()#get values from Entry boxes\n",
    "    reDraw(tolS,tolN)\n",
    "    \n",
    "root=Tk()\n",
    "\n",
    "reDraw.f = Figure(figsize=(5,4), dpi=100) #create canvas\n",
    "reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)\n",
    "reDraw.canvas.show()\n",
    "reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)\n",
    "\n",
    "Label(root, text=\"tolN\").grid(row=1, column=0)\n",
    "tolNentry = Entry(root)\n",
    "tolNentry.grid(row=1, column=1)\n",
    "tolNentry.insert(0,'10')\n",
    "Label(root, text=\"tolS\").grid(row=2, column=0)\n",
    "tolSentry = Entry(root)\n",
    "tolSentry.grid(row=2, column=1)\n",
    "tolSentry.insert(0,'1.0')\n",
    "Button(root, text=\"ReDraw\", command=drawNewTree).grid(row=1, column=2, rowspan=3)\n",
    "chkBtnVar = IntVar()\n",
    "chkBtn = Checkbutton(root, text=\"Model Tree\", variable = chkBtnVar)\n",
    "chkBtn.grid(row=3, column=0, columnspan=2)\n",
    "\n",
    "reDraw.rawDat = mat(loadDataSet('sine.txt'))\n",
    "reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)\n",
    "reDraw(1.0, 10)\n",
    "               \n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
